package com.wuhan.template.module.zixun.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseFragment;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.find.adapter.MeishiListAdapter;
import com.wuhan.template.module.find.adapter.MeishiPagerAdapter;
import com.wuhan.template.module.find.bean.MeishiBean;
import com.wuhan.template.module.home.listener.MyViewPageListener;
import com.wuhan.template.module.zixun.adapter.ZixunListAdapter;
import com.wuhan.template.module.zixun.bean.ZixunBean;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/16.
 */
public class ZixunCommonFragment extends BaseFragment {

    @BindView(R.id.zx_list)
    PullToRefreshListView listView;

    private String[] titles = {"头条", "武汉", "活动", "原创", "见微", "教育", "问政"
            , "热点", "图片", "广电"};
    private List<String> picUrls = new ArrayList<>();
    private MeishiPagerAdapter pagerAdapter;
    private ViewPager mVp;
    private List<ZixunBean.ListBean> mList = new ArrayList<>();
    private Handler handler = new Handler();
    private ZixunListAdapter listAdapter;
    private TextView mVpTitle;
    private RadioGroup mRg;
    private int i = 0;
    private List<String> contentUrls = new ArrayList<>();
    private String url = "";
    private View header;
    private Map<String, String> params = new HashMap<>();

    public ZixunCommonFragment()
    {

    }
    @Override
    protected int getLayoutId() {
        return R.layout.frag_zixun_common_layout;
    }

    @Override
    protected void loadData() {
        params.put("头条", "7");
        params.put("武汉", "8");
        params.put("活动", "73");
        params.put("原创", "61");
        params.put("见微", "135");
        params.put("教育", "77");
        params.put("问政", "87");
        params.put("热点", "55");
        params.put("图片", "32");
        params.put("广电", "57");

        Bundle bundle = getArguments();
        String p = bundle.getString("position");
        String columnId = params.get(titles[Integer.parseInt(p)]);
        try {
            url = Urls.PRE_OF_URL
                    + "columnId=" + columnId
                    + "&column_name=" + URLEncoder.encode(titles[Integer.parseInt(p)],"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.e("initViews", "==================loadData==============================");
        OkHttpUtils.get(url, null);
    }

    @Override
    protected void initViews() {

        Log.e("initViews", "==================initViews==============================");
        listAdapter = new ZixunListAdapter(getActivity(), mList);
        listView.setAdapter(listAdapter);

        AbsListView.LayoutParams layoutParams =
                new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        header = getActivity().getLayoutInflater().inflate(R.layout.meishi_viewpager_layout, listView, false);
        header.setLayoutParams(layoutParams);
        ListView lv = listView.getRefreshableView();
        lv.addHeaderView(header);

        mVp = (ViewPager) header.findViewById(R.id.vp_meishi);
        mVpTitle = (TextView) header.findViewById(R.id.tv_meishi);
        mRg = (RadioGroup) header.findViewById(R.id.ms_rg);

        pagerAdapter = new MeishiPagerAdapter(picUrls, contentUrls, getActivity());
        mVp.setAdapter(pagerAdapter);

    }

    @Override
    public void postEventResult(EventMessage message)
    {
        if (message.type.equals(url))
        {
            Log.e("postEventResult", "url === " + url);

            Gson gson = new Gson();
            ZixunBean zixunBean = gson.fromJson(message.result, ZixunBean.class);
            List<String> vpTitles = new ArrayList<>();
            for (ZixunBean.SlideBean slide : zixunBean.getSlide()) {
                picUrls.add(slide.getIndexpic().getHost()
                        + slide.getIndexpic().getDir()
                        + slide.getIndexpic().getFilepath()
                        + slide.getIndexpic().getFilename());
                contentUrls.add(slide.getContent_url());
                vpTitles.add(slide.getTitle());
            }

            mList.addAll(zixunBean.getList());
            listAdapter.notifyDataSetChanged();
            pagerAdapter.notifyDataSetChanged();
            addTextAndPoints(vpTitles);

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mVp.setCurrentItem(i++);
                    handler.postDelayed(this, 2500);
                }
            }, 2500);
        }


    }

    private void addTextAndPoints(final List<String> vpTitles) {
        int pointNum = vpTitles.size();
        for (int i = 0; i < pointNum; i++) {
            RadioButton tempButton = new RadioButton(getActivity());
            tempButton.setPadding(40, 0, 0, 0);
            tempButton.setButtonDrawable(R.drawable.point_radion_selector);           // 设置按钮的样式
            tempButton.setId(i);

            mRg.addView(tempButton);
        }
        mVpTitle.setText(vpTitles.get(0));
        mRg.check(0);
        mVp.addOnPageChangeListener(new MyViewPageListener() {

            @Override
            public void onPageSelected(int position) {
                mVpTitle.setText(vpTitles.get(position));
                mRg.check(position);

            }
        });


    }


}

